const { showFail } = require("../utils/util");

/**
 * GET请求封装
 */
function get(url, data = {}, header = {}) {
    return request(url, data, 'GET', header)
}

/**
 * POST请求封装
 */
function post(url, data = {}) {
    return request(url, data, 'POST')
}

function request(url, data = {}, method = "GET", header = {}) {
    var contentType = 'application/json'
    return new Promise(function (resolve, reject) {
        wx.request({
            url: url,
            data: data,
            method: method,
            header: {
                'Content-Type': contentType,
                // 'Authorization': wx.getStorageSync('easy_schedule_token'),
                'Authorization': header.Authorization,
                // 'Authorization': 'APPCODE 97335b8513ee4448a384a31e86861bb3',

            },
            success: function (res) {
                // console.log(res)
                if (res.statusCode == 200) {
                    if (res.data.code == 0 || 200) {
                        resolve(res.data.data);
                    }
                    // else if (res.data.code == 200)
                    else {
                        let reg = /uid不能为空/
                        if (reg.test(res.data.msg)) {
                            res.data.msg = '您未登录'
                        }
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        });
                        resolve()
                    }

                } else if (res.statusCode == 401) {
                    reject("登录已过期")
                    wx.showModal({
                        title: '提示',
                        content: '登录已过期，请立即登录，否则无法正常使用',
                        success(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                                wx.navigateTo({
                                    url: '/pages/home/me',
                                })
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })
                } else {
                    //请求失败
                    reject(res)
                }
            },
            fail: function (err) {
                reject(err)
            }
        })
    });
}

function postall(url, data = {}, method = "POST") {
    var contentType = 'application/json'
    return new Promise(function (resolve, reject) {
        wx.request({
            url: url,
            data: data,
            method: method,
            header: {
                'Content-Type': contentType,
                'Authorization': wx.getStorageSync('easy_schedule_token')
            },
            success: function (res) {
                // console.log(res)
                if (res.statusCode == 200) {
                    resolve(res.data);

                } else if (res.statusCode == 401) {
                    wx.showModal({
                        title: '提示',
                        content: '登录已过期，请立即登录，否则无法正常使用',
                        success(res) {
                            if (res.confirm) {
                                wx.navigateTo({
                                    url: '/pages/home/me',
                                })
                            } else if (res.cancel) {
                                wx.switchTab({
                                    url: '/pages/home/home',
                                    success: (result) => {

                                    },
                                    fail: () => { },
                                    complete: () => { }
                                });
                            }
                        }
                    })
                } else {
                    //请求失败
                    showFail('请求结果错误')
                }
            },
            fail: function (err) {
                showFail('请求发送失败')
            }
        })
    });
}

module.exports = {
    request,
    get,
    post,
    postall
}